﻿using ArticleContentCreate.Utils;
using ArticleContentCreate.Utils.View;
using CatUtils.Utils.Currency;
using CatUtils.Utils.Currency.IoUtils;
using CatUtils.Utils.Currency.TextUtils;
using CatUtils.Utils.Helper;
using CatUtils.Utils.SpecialUtils.Html;
using CatUtils.Utils.SpecialUtils.View;
using CatUtilsDll.Utils.Currency.WebUtils;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ArticleContentCreate.WebSite.LocalMode
{
    internal class LocalImgGet
    {
        public string LocalImgPath = LocalStaticvalue.WebSiteInfo + "PixivLocal\\";

        public void AddData()
        {
            FloderHelper.FloderExits(LocalImgPath, true);
            FloderHelper.OpenFloder(LocalImgPath);
        }

        public void GetImgFile(string GetImgCountText)
        {
            int GetImgCount = TextHelper.StringToInt(GetImgCountText);
            GetImgFile(GetImgCount);
        }

        public void GetImgFile(int GetImgCount)
        {
            List<string> ALLImgList = FloderNameHelper.GetFullFileNameList(LocalImgPath);
            if (ALLImgList.Count == 0)
            {
                MessageHelper.Alert("没有本地资源，请重新导入！");
                return;
            }
            string RandomFilePath = ListHelper.Ramdom(ALLImgList, true);
            string Tip = string.Format("你想发表{0}的动漫图集吗？", FileNameHelper.GetFileNameWithoutExtension(RandomFilePath));

            if (MessageHelper.DialogResultAlert(Tip))
            {
                ReadFile(RandomFilePath, GetImgCount);
            }
            else
            {
                if (MessageHelper.DialogResultAlert("获取下一个图集？"))
                {
                    GetImgFile(GetImgCount);
                }
                else
                {
                    return;
                }
            }
        }

        /// <summary>
        /// 启动下载
        /// </summary>
        /// <param name="FilePath"></param>
        private void ReadFile(string FilePath, int GetImgCount)
        {
            Console.WriteLine("打印日志：" + FilePath);
            List<String> ImgList = HtmlHelper.GetHtmlValueList(FileHelper.ReadAllText(FilePath), "//bed");
            List<String> TempList = new List<string>();
            if (ImgList.Count < GetImgCount)
            {
                if (!MessageHelper.DialogResultAlert(string.Format("该动漫图片仅有{0}张，是否下载？", ImgList.Count)))
                {
                    FileHelper.FileDelete(FilePath);
                    return;
                }
            }
            if (ImgList.Count == 0 || ImgList == null)
            {
                FileHelper.FileDelete(FilePath);
                return;
            }
            for (int i = 0; i < GetImgCount && ImgList.Count > 0; i++)
            {
                int GetRandom = TextHelper.GetRandom(ImgList.Count);
                string Info = ImgList[GetRandom];
                if (Info == "")
                    continue;
                TempList.Add(HtmlHelper.GetFirstText(Info, "//sina"));
                ImgList.RemoveAt(GetRandom);
            }
    ;
            foreach (var item in TempList)
            {
                Console.WriteLine(item);
            }
            Console.WriteLine(TempList.Count);
            DownloadList(TempList, FilePath);
            if (ImgList.Count > 0)
                FileHelper.WriteList(FilePath, ImgList);
            else FileHelper.FileDelete(FilePath);
        }

        private void DownloadList(List<string> tempList, string FilePath)
        {
            int CompltedCount = 0;
            string FileName = FileNameHelper.GetFileNameWithoutExtension(FilePath) + "\\";
            string SavePath = LocalStaticvalue.ImgTemp1 + FileName;
            FloderHelper.FloderExits(SavePath, true);
            foreach (var item in tempList)
            {
                new PrintLog().PercentLog(tempList.Count, CompltedCount++);
                new WebHelper().DownloadToFile(item, SavePath);
            }
            new PropagandaTool().CreatePublishAD(SavePath);
            new MessageHelper().OpenFloderAlert(SavePath);
        }
    }
}